import { GImageCell } from "../../../../com/mui/controls/GImageCell";
import { IDragDrop } from "../../../../com/mui/manager/IDragDrop";
import { SecTimer } from "../../../../com/gengine/core/frame/SecTimer";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { ECategory } from "../../../../Message/Public/ECategory";
import { ItemData } from "../../resource/info/ItemData";
import { CDItemData } from "../../cache/CDItem/CDItemData";
import { GTextFormat } from "../../../common/GTextFormat";
import { FontUtil } from "../../../common/font/FontUtil";
import { ObjCreate } from "./ObjCreate";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { FilterConst } from "../../../component/gconst/FilterConst";
import { ScaleBitmap } from "../../../../com/mui/display/ScaleBitmap";
import { BitmapDataConst } from "../../../common/display/BitmapDataConst";
import { FreezingEffect } from "./FreezingEffect";
import { LeftTimeCd } from "./LeftTimeCd";
import { ToolTipsManager } from "../../../../com/mui/manager/ToolTipsManager";
import { DragManager } from "../../../../com/mui/manager/DragManager";
import { LoaderManager } from "../../../../com/gengine/resource/LoaderManager";
import { LoaderPriority } from "../../../../com/gengine/resource/LoaderPriority";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { Language } from "../../../../extend/language/Language";
import { ImageInfo } from "../../../../com/gengine/resource/info/ImageInfo";
import { ObjectPool } from "../../../../com/gengine/utils/pools/ObjectPool";
import { DisplayUtil } from "../../../common/DisplayUtil";
import { EPlayerItemPosType } from "../../../../Message/Public/EPlayerItemPosType";
import { ClockManager } from "../../manager/ClockManager";
import { TimerType } from "../../../../com/gengine/core/frame/TimerType";
import { GTextFiled } from "../../../../com/mui/controls/GTextFiled";
type int = number;
//class BaseItem
    
    export  class BaseItem extends GImageCell implements IDragDrop
    {
        constructor()
        {
            super();
            this.createChildren();
            return;
        }

        /* internal  */onTimerHandler(arg1: SecTimer): void
        {
            --this._time;
            if (this._time <= 0) 
            {
                this.hasExpired = true;
                this._timer.stop();
                this._timer.dispose();
            }
            return;
        }

        protected set isBind(arg1: Boolean)
        {
            this._isBind = arg1;
            if (!this._isShowLock) 
            {
                return;
            }
            if (this._isBind) 
            {
                if (!this.lockedIcon) 
                {
                    this.lockedIcon = GlobalClass.getBitmap("Lock");
                    addChild(this.lockedIcon);
                }
            }
            if (this.lockedIcon) 
            {
                this.lockedIcon.visible = this._isBind;
            }
            return;
        }

        public get isDropAble(): Boolean
        {
            return this._dropAble;
        }

        public set isDropAble(arg1: Boolean)
        {
            this._dropAble = arg1;
            return;
        }

        public get isDragAble(): Boolean
        {
            return this._dragAble;
        }

        public set isDragAble(arg1: Boolean)
        {
            this._dragAble = arg1;
            if (this._dragAble) 
            {
                this.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.onMouseDown);
            }
            else if (this.hasEventListener(flash.events.MouseEvent.MOUSE_DOWN)) 
            {
                this.removeEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.onMouseDown);
            }
            return;
        }

        public get isThrowAble(): Boolean
        {
            return this._throwAble;
        }

        public set isThrowAble(arg1: Boolean)
        {
            this._throwAble = arg1;
            return;
        }

        public canDrop(arg1: IDragDrop, arg2: IDragDrop): Boolean
        {
            if (this.dropChecktFunction != null) 
            {
                return this.dropChecktFunction(arg1, this);
            }
            return this.isDropAble;
        }

        public /* override */ get toolTipData()
        {
            if (!this.isShowToolTip) 
            {
                return null;
            }
            if (toolTipDataFunction != null) 
            {
                return this.toolTipDataFunction(this);
            }
            if (this.itemData) 
            {
                if (this.itemData.category == ECategory._ECategoryPetEquip) 
                {
                    return this.itemData.getToolTipData(ItemData.ToolTipType_PetEquip);
                }
                if (this.itemData.category == ECategory._ECategoryMountEquip) 
                {
                    return this.itemData.getToolTipData(ItemData.ToolTipType_MountEquip);
                }
                return this.itemData.getToolTipData(this.toolTipType);
            }
            return super.toolTipData;
        }

        public /* override */ setSize(arg1: number, arg2: number): void
        {
            super.setSize(arg1, arg2);
            this.imageWidth = arg1;
            this.imageHeight = arg2;
            this._width = arg1;
            this._height = arg2;
            this.resetBitmapSize();
            if (this.freezingEffect) 
            {
                this.freezingEffect.x = 18;
                this.freezingEffect.y = 18;
                this.freezingEffect.setMaskSize(_width, _height);
            }
            if (this.leftTimeCd) 
            {
                this.leftTimeCd.x = 0;
                this.leftTimeCd.y = (_height - 18) / 2;
                this.leftTimeCd.width = _width;
                this.leftTimeCd.height = 20;
            }
            if (this.amountLabel) 
            {
                this.amountLabel.width = arg1;
            }
            if (this.lockedEffect) 
            {
                this.lockedEffect.width = arg1;
                this.lockedEffect.height = arg2;
            }
            if (!this.lockedIcon) 
            {
            };
            if (this.usedEffect) 
            {
                this.usedEffect.width = arg1;
                this.usedEffect.height = arg2;
            }
            if (this.noDurabilityEffect) 
            {
                this.noDurabilityEffect.width = arg1;
                this.noDurabilityEffect.height = arg2;
            }
            if (this.notMeetCareerEffect) 
            {
                this.notMeetCareerEffect.width = arg1;
                this.notMeetCareerEffect.height = arg2;
            }
            if (this.hasExpiredEffect) 
            {
                this.hasExpiredEffect.width = arg1;
                this.hasExpiredEffect.height = arg2;
            }
            return;
        }

        public /* override */ get width(): number
        {
            return this._width;
        }

        protected get isBind(): Boolean
        {
            return this._isBind;
        }

        public /* override */ get height(): number
        {
            return this._height;
        }

        public /* override */ set label(arg1: string)
        {
            if (arg1 != null) 
            {
                super.label = arg1;
            }
            return;
        }

        public get playingEffect(): Boolean
        {
            return this.freezingEffect.isCDPlaying;
        }

        public get cdTime(): CDItemData
        {
            return this.freezingEffect ? this.freezingEffect.cdTime : null;
        }

        protected createChildren(): void
        {
            this._vTf = new GTextFormat();
            this._vTf.align = flash.text.TextFormatAlign.RIGHT;
            this._vTf.color = 16776960;
            this._vTf.bold = true;
            this._vTf.size = 14;
            this._vTf.font = FontUtil.EmbedNumberName;
            this.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.onMouseDown);
            this._bitmap = new Object /* flash.display.Bitmap */();
            this.addChild(this._bitmap);
            this.resetBitmapSize();
            super.label = "";
            this.label = "";
            this.amountLabel = new Object /* flash.text.TextField */();
            this.amountLabel.selectable = false;
            this.amountLabel.mouseEnabled = false;
            this.amountLabel.width = 20;
            this.amountLabel.height = 20;
            this.amountLabel.y = 20;
            addChild(this.amountLabel);
            this.tfNotIdentify = ObjCreate.createGTextField("", 18, 20, 15, 20, this, GlobalStyle.textFormat6, false);
            this.tfNotIdentify.selectable = false;
            this.tfNotIdentify.mouseEnabled = false;
            this.tfNotIdentify.autoSize = flash.text.TextFieldAutoSize.RIGHT;
            var loc1=new GTextFormat();
            loc1.align = flash.text.TextFormatAlign.RIGHT;
            loc1.color = 16777215;
            loc1.size = 11;
            this.amountLabel.defaultTextFormat = loc1;
            this.amountLabel.autoSize = flash.text.TextFieldAutoSize.NONE;
            this.amountLabel.filters = [FilterConst.glowFilter];
            var loc2=new Object /* flash.display.Sprite */();
            loc2.graphics.beginFill(16711680, 0);
            loc2.graphics.drawRect(0, 0, 10, 10);
            this.setStyle("upSkin", loc2);
            this.setStyle("downSkin", loc2);
            this.setStyle("overSkin", loc2);
            this.setStyle("selectedDownSkin", loc2);
            this.setStyle("selectedOverSkin", loc2);
            this.setStyle("selectedUpSkin", loc2);
            this.lockedEffect = new ScaleBitmap(BitmapDataConst.LockedBitmapData);
            this.addChild(this.lockedEffect);
            this.lockedEffect.visible = this._locked;
            this.usedEffect = new ScaleBitmap(BitmapDataConst.UsedBmd);
            this.addChild(this.usedEffect);
            this.usedEffect.visible = this._used;
            this.noDurabilityEffect = new ScaleBitmap(BitmapDataConst.NoDurabilityBMD);
            this.addChild(this.noDurabilityEffect);
            this.noDurabilityEffect.visible = false;
            this.notMeetCareerEffect = new ScaleBitmap(BitmapDataConst.NotMeetCareerBMD);
            this.addChild(this.notMeetCareerEffect);
            this.notMeetCareerEffect.visible = this._meetCareer;
            this.hasExpiredEffect = new ScaleBitmap(BitmapDataConst.hasExpiredBMD);
            this.addChild(this.hasExpiredEffect);
            this.hasExpiredEffect.x = 1;
            this.hasExpiredEffect.y = 1;
            this.hasExpiredEffect.visible = false;
            this.freezingEffect = new FreezingEffect();
            addChild(this.freezingEffect);
            this.leftTimeCd = new LeftTimeCd();
            this.leftTimeCd.filters = [FilterConst.glowFilter];
            var loc3=GlobalStyle.textFormat6;
            loc3.align = flash.text.TextFormatAlign.CENTER;
            loc3.size = 13;
            this.leftTimeCd.defaultTextFormat = loc3;
            if (this._isShowCdLeftTime) 
            {
                addChild(this.leftTimeCd);
            }
            this.effectLayer = new Object /* flash.display.Sprite */();
            addChild(this.effectLayer);
            this.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddToStage);
            this.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onRemoveFromStage);
            return;
        }

        /* internal  */onAddToStage(arg1: Object /* flash.events.Event */): void
        {
            ToolTipsManager.register(this);
            return;
        }

        /* internal  */onRemoveFromStage(arg1: Object /* flash.events.Event */): void
        {
            ToolTipsManager.unregister(this);
            if (this._timer) 
            {
                this._timer.stop();
                this._timer.dispose();
            }
            return;
        }

        protected onMouseDown(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this.itemData && this.isDragAble && !this._locked && !this._used) 
            {
                DragManager.instance.startDragItem(this, this.bitmapdata);
            }
            return;
        }

        public /* override */ set source(arg1: Object)
        {
            if (this._lastImageUrl) 
            {
                LoaderManager.instance.removeResourceEvent(this._lastImageUrl, this.onLoadCompleteHandler);
                this._lastImageUrl = "";
            }
            if (arg1 is flash.display.Bitmap) 
            {
                this._bitmapdata = (arg1 as Object /* flash.display.Bitmap */).bitmapData;
                this._bitmap.bitmapData = this._bitmapdata;
                this.resetBitmapSize();
                this.amountLabel.visible = true;
                return;
            }
            if (arg1 is flash.display.BitmapData) 
            {
                this._bitmap.bitmapData = arg1 as Object /* flash.display.BitmapData */;
                this.resetBitmapSize();
                this.amountLabel.visible = true;
                return;
            }
            this._bitmap.bitmapData = this._defaultBitmapData;
            if (typeof arg1 === "string") 
            {
                this.imageUrl = arg1 as string;
                this._lastImageUrl = this.imageUrl;
                LoaderManager.instance.load(this.imageUrl, this.onLoadCompleteHandler, LoaderPriority.LevelB);
            }
            else if (this.imageUrl) 
            {
                this.imageUrl = null;
            }
            return;
        }

        protected get strengthLabel(): Object /* flash.text.TextField */
        {
            if (!this._strengthLabel) 
            {
                this._strengthLabel = ObjCreate.createTextField("", -8, 17, 42, 20, this, this._vTf);
                this._strengthLabel.selectable = false;
                this._strengthLabel.mouseEnabled = false;
                this._strengthLabel.embedFonts = true;
                this._strengthLabel.autoSize = flash.text.TextFieldAutoSize.RIGHT;
            }
            return this._strengthLabel;
        }

        protected set strengthNum(arg1: int)
        {
            this._strengthNum = arg1;
            if (this._strengthNum > 0) 
            {
                this.strengthLabel.text = "+" + arg1;
                this.strengthLabel.visible = true;
            }
            else 
            {
                this.strengthLabel.visible = false;
            }
            return;
        }

        protected setTfNotIdentify(): void
        {
            if (ItemsUtil.isMagicWeapon(this._itemData) && this._itemData.itemCode > 0) 
            {
                if (this._itemData.isHasIdentify()) 
                {
                    this.tfNotIdentify.y = 2;
                    this.tfNotIdentify.embedFonts = true;
                    this.tfNotIdentify.defaultTextFormat = this._vTf;
                    this.tfNotIdentify.text = this._itemData.magicWeaponScore.toString();
                }
                else 
                {
                    this.tfNotIdentify.y = 20;
                    this.tfNotIdentify.embedFonts = false;
                    this.tfNotIdentify.defaultTextFormat = GlobalStyle._textFormat6;
                    this.tfNotIdentify.text = Language.getString(40103);
                }
            }
            else 
            {
                this.tfNotIdentify.text = "";
            }
            return;
        }

        protected onLoadCompleteHandler(arg1: ImageInfo): void
        {
            this._lastImageUrl = "";
            this._bitmapdata = arg1.bitmapData;
            this._bitmap.bitmapData = this._bitmapdata;
            this.resetBitmapSize();
            this.amountLabel.visible = true;
            return;
        }

        protected /* override */ handleErrorEvent(arg1: Object /* flash.events.IOErrorEvent */): void
        {
            dispatchEvent(arg1);
            return;
        }

        public removeEffect(): void
        {
            while (this.effectLayer.numChildren > 0) 
            {
                this.effectLayer.removeChildAt(0);
            }
            return;
        }

        public get bitmapdata(): Object /* flash.display.BitmapData */
        {
            var loc1=NaN;
            if (this._bitmapdata) 
            {
                if (this._bitmapdata.width > this.width + 5 || this._bitmapdata.height > this.height + 5) 
                {
                    loc1 = this.width / this._bitmapdata.width;
                    this._bitmapdata = new Object /* flash.display.BitmapData */(this.width, this.height, true, 0);
                    this._bitmapdata.draw(this._bitmap, new Object /* flash.geom.Matrix */(loc1, 0, 0, loc1, 0, 0));
                }
            }
            return this._bitmapdata;
        }

        public get amount(): int
        {
            return this._amount;
        }

        public set amount(arg1: int)
        {
            if (this._amount != arg1) 
            {
                this._amount = arg1;
                if (this._amount <= 1) 
                {
                    this.amountLabel.text = "";
                }
                else 
                {
                    this.amountLabel.text = this._amount + "";
                }
            }
            return;
        }

        public setAmountText(arg1: int): void
        {
            if (arg1 != -1) 
            {
                this.amountLabel.text = arg1 + "";
            }
            else 
            {
                this.amountLabel.text = "";
            }
            return;
        }

        protected resetBitmapSize(): void
        {
            if (this._bitmap) 
            {
                this._bitmap.width = _width;
                this._bitmap.height = _height;
            }
            return;
        }

        public get isShowLock(): Boolean
        {
            return this._isShowLock;
        }

        public set isShowLock(arg1: Boolean)
        {
            this._isShowLock = arg1;
            return;
        }

        public showLightMask(): void
        {
            if (!this._lightMask) 
            {
                this._lightMask = ObjectPool.getObject(LightMask);
                this._lightMask.transform.matrix = new Object /* flash.geom.Matrix */();
            }
            addChild(this._lightMask);
            return;
        }

        public hideLightMask(): void
        {
            if (this._lightMask) 
            {
                if (contains(this._lightMask)) 
                {
                    removeChild(this._lightMask);
                }
                this._lightMask.width = 40;
                this._lightMask.height = 40;
                ObjectPool.disposeObject(this._lightMask);
            }
            return;
        }

        public setAmountLabelPos(arg1: int, arg2: int): void
        {
            this.amountLabel.x = arg1;
            this.amountLabel.y = arg2;
            return;
        }

        public setStrengthLabel(arg1: int, arg2: int): void
        {
            this.strengthLabel.x = arg1;
            this.strengthLabel.y = arg2;
            return;
        }

        public setAmountLabelFormat(arg1: Object /* flash.text.TextFormat */): void
        {
            this.amountLabel.defaultTextFormat = arg1;
            return;
        }

        public set dragSource(arg1: Object)
        {
            if (typeof arg1 === "itemdata") 
            {
                this.itemData = arg1 as ItemData;
            }
            return;
        }

        public setAmountLabel(arg1: string): void
        {
            this.amountLabel.htmlText = arg1;
            return;
        }

        public get isShowCdLeftTime(): Boolean
        {
            return this._isShowCdLeftTime;
        }

        public set isShowCdLeftTime(arg1: Boolean)
        {
            this._isShowCdLeftTime = arg1;
            if (this.leftTimeCd) 
            {
                if (arg1) 
                {
                    this.addChild(this.leftTimeCd);
                }
                else 
                {
                    DisplayUtil.removeMe(this.leftTimeCd);
                }
            }
            return;
        }

        public get meetCareer(): Boolean
        {
            return this._meetCareer;
        }

        public set meetCareer(arg1: Boolean)
        {
            this._meetCareer = arg1;
            this.notMeetCareerEffect.visible = this._meetCareer;
            return;
        }

        public set haveDurability(arg1: Boolean)
        {
            this._haveDurability = arg1;
            this.noDurabilityEffect.visible = !this._haveDurability;
            return;
        }

        public set hasExpired(arg1: Boolean)
        {
            this._hasExpired = arg1;
            this.hasExpiredEffect.visible = this._hasExpired;
            return;
        }

        public get used(): Boolean
        {
            return this._used;
        }

        public set used(arg1: Boolean)
        {
            this._used = arg1;
            this.usedEffect.visible = this._used;
            return;
        }

        public get locked(): Boolean
        {
            return this._locked;
        }

        public set locked(arg1: Boolean)
        {
            this._locked = arg1;
            this.lockedEffect.visible = arg1;
            return;
        }

        public get dragSource(): Object
        {
            return this.itemData;
        }

        public addEffect(arg1: string): void
        {
            return;
        }

        public get toolTipRenderClass(): Class
        {
            return this._toolTipRenderClass;
        }

        public set toolTipRenderClass(arg1: Class)
        {
            this._toolTipRenderClass = arg1;
            return;
        }

        public set strengthLabelVisible(arg1: Boolean)
        {
            this.strengthLabel.visible = arg1;
            return;
        }

        public get itemData(): ItemData
        {
            return this._itemData;
        }

        public set itemData(arg1: ItemData)
        {
            var loc1=0;
            var loc2=null;
            var loc3=0;
            if (arg1) 
            {
                this._itemData = arg1;
                this.source = arg1.url;
                this.amount = arg1.itemAmount;
                this.isBind = ItemsUtil.isBind(this._itemData);
                this.haveDurability = !((this._itemData.posType == EPlayerItemPosType._EPlayerItemPosTypeRole || this._itemData.posType >= EPlayerItemPosType._EPlayerItemPosTypePetEquipBase) && (this.itemData.category == ECategory._ECategoryEquip || this.itemData.category == ECategory._ECategoryPetEquip) && this.itemData.itemExInfo.durable <= 0);
                this.hasExpired = ItemsUtil.isHasExpired(arg1);
                this.setTfNotIdentify();
                if (this._itemData.category == ECategory._ECategoryMount && this._itemData.itemExInfo && this._itemData.itemExInfo.m_adv_lv) 
                {
                    this.strengthNum = this._itemData.itemExInfo.m_adv_lv;
                }
                else if (ItemsUtil.isMagicWeapon(this._itemData) && this._itemData.itemExInfo && this._itemData.itemExInfo.al_lv) 
                {
                    this.strengthNum = this._itemData.itemExInfo.al_lv;
                }
                else 
                {
                    this.strengthNum = ItemsUtil.getEquipStrengthen(this.itemData);
                }
                if (this.itemData && this.itemData.itemExInfo && ItemsUtil.isDrugNewExp(this.itemData)) 
                {
                    loc1 = this.itemData.itemExInfo.tm_lm_exp_et;
                    loc2 = ClockManager.instance.nowDate;
                    loc3 = loc1 - loc2.time / 1000;
                    this._time = loc3;
                    if (loc3 <= 0) 
                    {
                        this.hasExpired = true;
                    }
                    else 
                    {
                        this.isOverTime();
                    }
                }
            }
            else 
            {
                this.source = null;
                this.amount = 0;
                this._itemData = null;
                this.isBind = false;
                this.haveDurability = true;
                this.hasExpired = false;
                this.strengthNum = 0;
                this.tfNotIdentify.text = "";
            }
            return;
        }

        /* internal  */isOverTime(): void
        {
            this.startTimerCount();
            return;
        }

        /* internal  */startTimerCount(): void
        {
            this._timer = new SecTimer();
            this._timer.addListener(TimerType.ENTERFRAME, this.onTimerHandler);
            this._timer.start();
            return;
        }

        public static /* const */Event_EffectComplete: string="冷却动画播放结束";

        public /* var */posIndex: int=0;

        public /* var */dropBaseType: Object;

        public /* var */toolTipType: int=0;

        protected /* var */amountLabel: Object /* flash.text.TextField */;

        protected /* var */freezingEffect: FreezingEffect;

        protected /* var */leftTimeCd: LeftTimeCd;

        protected /* var */lockedEffect: ScaleBitmap;

        protected /* var */lockedIcon: Object /* flash.display.Bitmap */;

        protected /* var */tfNotIdentify: GTextFiled;

        protected /* var */usedEffect: ScaleBitmap;

        protected /* var */noDurabilityEffect: ScaleBitmap;

        protected /* var */notMeetCareerEffect: ScaleBitmap;

        protected /* var */_source: Object="";

        protected /* var */_strengthLabel: Object /* flash.text.TextField */;

        protected /* var */imageWidth: number=-1;

        protected /* var */imageHeight: number=-1;

        protected /* var */imageUrl: string;

        protected /* var */effectLayer: Object /* flash.display.Sprite */;

        public /* var */isShowToolTip: Boolean=true;

        /* internal */ /* var */_bitmapdata: Object /* flash.display.BitmapData */=null;

        /* internal */ /* var */_defaultBitmapData: Object /* flash.display.BitmapData */;

        protected /* var */_bitmap: Object /* flash.display.Bitmap */=null;

        /* internal */ /* var */_isShowLock: Boolean=false;

        /* internal */ /* var */_isShowCdLeftTime: Boolean=false;

        /* internal */ /* var */_lightMask: LightMask;

        /* internal */ /* var */_vTf: GTextFormat;

        public /* var */dropChecktFunction: Function;

        protected /* var */_itemData: ItemData;

        protected /* var */_dragAble: Boolean=true;

        protected /* var */_dropAble: Boolean=true;

        protected /* var */_throwAble: Boolean=true;

        protected /* var */_toolTipRenderClass: Class;

        protected /* var */_amount: int=0;

        protected /* var */_locked: Boolean=false;

        public /* var */posType: int=0;

        protected /* var */_haveDurability: Boolean=true;

        protected /* var */_hasExpired: Boolean=false;

        /* internal */ /* var */_isBind: Boolean=false;

        protected /* var */hasExpiredEffect: ScaleBitmap;

        protected /* var */_meetCareer: Boolean=false;

        protected /* var */_used: Boolean=false;

        /* internal */ /* var */_timer: SecTimer;

        /* internal */ /* var */_time: int;

        /* internal */ /* var */_lastImageUrl: string;

        protected /* var */_strengthNum: int=0;
    }
